﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Model;
using LogisticsBLL;

namespace WebLosgic
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int UserID = Convert.ToInt32(Request.QueryString["Uid"]);
                if (UserID != 0)
                {
                    DdlBind();
                    GetBind();
                }
                else
                {
                    Response.Redirect("Users.aspx");
                }
            }
        }

        public void DdlBind()
        {
            SqlDataReader read = UserBLL.droplie();
            DropRole.DataSource = read;
            DropRole.DataTextField = "RoleName";
            DropRole.DataValueField = "RoleID";
            DropRole.DataBind();
            ListItem item = new ListItem();
            item.Text = "-请选择-";
            item.Value = "0";
            DropRole.Items.Add(item);
            DropRole.SelectedValue = "0";
        }

        public void GetBind()
        {
            int UserID = Convert.ToInt32(Request.QueryString["Uid"]);
            UserBLL ubl = new UserBLL();
            Model.Users us = ubl.UserBind(UserID);
            username.Value = us.UserName;
            RadioSex.SelectedValue = us.sex.ToString();
            useremail.Value = us.Email;
            userphone.Value = us.phone;
            userQ.Value = us.Account;
            DropRole.SelectedValue = us.RoleID.ToString();
            RadioIsdel.SelectedValue = us.IsDelete.ToString();
        }

        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Users.aspx");
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(Request.QueryString["Uid"]);
            string Acount = userQ.Value;
            string UserName = username.Value;
            string Sex = RadioSex.SelectedValue;
            string Phone = userphone.Value;
            string Email = useremail.Value;
            string RoleID = DropRole.SelectedValue;
            int IsDel = Convert.ToInt32(RadioIsdel.SelectedValue);
            int result = UserBLL.upd(UserName, Acount, Sex, Phone, Email, RoleID, IsDel, UserID);
            if (result > 0)
            {
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 修改用户信息";
                int TypeId = 4;
                int UserIDs = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + UserName + "," + Acount + "," + Sex + "," + Email;
                string ProcName = "p_Update";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserIDs, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改成功！');location.href='Users.aspx';", true);
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改失败！')", true);
            }
            
        }
    }
}